<?php
final class aInput {
	
	private static $input;
	private static $killlist;
	private static $magicquotes;
	private static $request_method;
	private static $methodlimit = array('post'=>array(),'get'=>array());

	private static function _input() {
		$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS");
		foreach($protected as $var) {
			if (isset($_REQUEST[$var]) || isset($_FILES[$var])) {
				die("Hacking attempt");
			} 
		} 
		// Determine Magic Quotes Status (< PHP 5.3)
		if (version_compare(PHP_VERSION, '5.3', '<')) {
			if (get_magic_quotes_gpc()) {
				self::$magicquotes = 1;
				self::strip_slashes_array($_POST);
				self::strip_slashes_array($_GET);
				self::strip_slashes_array($_COOKIE);
			} 
			set_magic_quotes_runtime(0);
			@ini_set("magic_quotes_gpc", 0);
			@ini_set("magic_quotes_runtime", 0);
		}
		//if(self::$only != 'get') {
			self::parse_incoming($_POST,'post');
		//} 
		//if(self::$only != 'post') {
			self::parse_incoming($_GET,'get');
		//}
		if ($_SERVER['REQUEST_METHOD'] == "POST") {
			self::$request_method = "post";
		} else if ($_SERVER['REQUEST_METHOD'] == "GET") {
			self::$request_method = "get";
		} 
		if (@ini_get("register_globals") == 1) {
			self::unset_globals($_POST);
			self::unset_globals($_GET);
			self::unset_globals($_FILES);
			self::unset_globals($_COOKIE);
		} 
	} 
	private static function parse_incoming($array,$method) {
		if (!is_array($array)) {
			return;
		} 

		foreach($array as $key => $val) {
			//if(!isset(self::$input[$key])) { // 禁止覆盖变量
				self::$input[$key] = $val;// 开始解析
				self::$methodlimit[$method][] = $key;
			//}
		} 
	} 
	private static function strip_slashes_array(&$array) {
		foreach($array as $key => $val) {
			if (is_array($array[$key])) {
				self::strip_slashes_array($array[$key]);
			} else {
				$array[$key] = stripslashes($array[$key]);// 去掉转义
			} 
		} 
	} 
	private static function unset_globals($array) {// 清理全局变量
		if (!is_array($array)) {
			return;
		} 

		foreach(array_keys($array) as $key) {
			unset($GLOBALS[$key]);
			unset($GLOBALS[$key]); // Double unset to circumvent the zend_hash_del_key_or_index hole in PHP <4.4.3 and <5.1.4
		} 
	} 
	
	public static function kill($value,$kill=true){
		if($value) {
			if(is_array($value)) {
				foreach($value as $val) {
					self::kill($val,$kill);
				}
			} else {
				if($kill == true) {
					self::$killlist[$value] = 1;
				} else {
					self::$killlist[$value] = 0;
				}
			}
		}
	}

	public static function init($value='',$method='all') {
		if (!$value) {
			return false;
		} 
		
		self::_input();// 初始化输入内容
		$init = $_val = null;

		if (is_array($value)) {
			foreach($value as $val) {
				if ($val && !is_array($val)) {
					if (isset(self::$input[$val])) {
						$_val[$val] = self::init($val,$method);
					}
				} 
			} 
			$init = $_val;
		} else {
			if (isset(self::$input[$value])) {
				if (self::$killlist[$value] == 1) {
					self::$input[$value] = null;
				}
				$_val = self::$input[$value];

				// 限制获取方式
				if ($method == 'all') {
					$init = $_val;
				} elseif ($method == 'get') {
					if(!in_array($value,self::$methodlimit['post'])) {
						$init = $_val;
					}
				} elseif ($method == 'post') {
					if(!in_array($value,self::$methodlimit['get'])) {
						$init = $_val;
					}
				}
				
				// 安全处理交给安全类
			} 
		}
		return $init;
	} 
} 
?>